<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="group9.UserStory" %>
	<%@ page import="group9.User" %>
	<%@ page import="group9.Estimate" %>
	<%@ page import="group9.PMF" %>
	<%@ page import="java.util.ArrayList" %>
	<%@ page import="java.util.List" %>
	<%@ page import="javax.jdo.PersistenceManager" %>
	<%@ page import="com.google.appengine.api.datastore.Key" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<!-- Claudio Herreros, Dylan James, Tyler Berg -->

<%
	User user = (User)session.getAttribute("User");
	
	if (user == null){
		response.sendRedirect("login.jsp");
	}
	
	//User user = (User) req.getSession().getAttribute("User");
	UserStory userstory = (UserStory)session.getAttribute("UserStory");

	if (userstory == null)
	{
		response.sendRedirect("MyUserStories.jsp");
	}
	PersistenceManager pm = PMF.get().getPersistenceManager();
	
	String query = "select from " + User.class.getName();
	List<User> userList = (List<User>) pm.newQuery(query).execute();
	
%>
	<head>
		<script type="text/javascript">
			function checkUsers(){
				var w = document.usernamesForm.usernamesList.selectedIndex;
				document.usernamesForm.estimatorsList.options[document.usernamesForm.estimatorsList.length]
			                                      		=new Option(document.usernamesForm.usernamesList.options[w].value);
				document.usernamesForm.usernamesList.remove(w);
			}
			function checkEstimators(){
				var w = document.usernamesForm.estimatorsList.selectedIndex;
				document.usernamesForm.usernamesList.options[document.usernamesForm.usernamesList.length]
					                                      		=new Option(document.usernamesForm.estimatorsList.options[w].value);
				document.usernamesForm.estimatorsList.remove(w);
				updateUsernames()
			}
			function updateUsernames(){
				for(i = 0; i < document.usernamesForm.estimatorsList.length; i++){
					document.usernamesForm.usernames.value +=  document.usernamesForm.estimatorsList.options[i].value + ","
				}
			}
		</script>
		<link rel="stylesheet" type="text/css" href="styles.css" title="Style 1" />
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Edit Estimators</title>
	</head>
	<body>
		<h1>Edit Estimators</h1>
		<a href="/SignOut" class="Signout">SignOut</a>
		<br/>
		<hr/>
		
		<form name="usernamesForm" action="/editEstimator" method="post" onsubmit="updateUsernames()">
			<table align="left"  border="0" cellspacing="5" cellpadding="10">
			<tr>
			<td><span class="BoxLabel">Usernames:</span></td>
			<td/><td/>
			<td><span class="BoxLabel">Estimators:</span></td>
			</tr>
			<tr>
			<td><select size="10" ondblclick="checkUsers()" name="usernamesList" style="width:200px">
			<% 
			for(int i = 0; i < userList.size(); i++){
				if(!userstory.existsEstimator(userList.get(i))){
					System.out.println("--"+userstory.existsEstimator(userList.get(i)));
					System.out.println("--"+userstory.existsEstimator(user) + " " + user.getKey());
				
				%>
				 <option><%=userList.get(i).getUserName()%></option>
			<% 
				}
			}
			%>
			</select></td>
			<td><input type="button" onclick="checkUsers()" value="Add"/></td>
			<td><input type="button" onclick="checkEstimators()" value="Remove"/></td>
			<td><select size="10" ondblclick="checkEstimators()" name="estimatorsList" style="width:200px">
			<% 
			for(int i = 0; i < userList.size(); i++){
				if(userstory.existsEstimator(userList.get(i))){
				
				%>
				 <option><%=userList.get(i).getUserName()%></option>
			<% 
				}
			}
			pm.close();
			%>
			</select></td>
			</tr>
			<tr>
			<td><input type="submit" value="Submit"></input>
			<input type="button"value="Cancel" onclick="location.href='MyUserStories.jsp'"></td>
			</tr>
			</table>
		<input type="hidden" name="usernames"/>
		</form>
	
	<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
	<p>*Users cannot be remove after they have been add as estimators</p>
		<hr/>
		<a href="MyUserStories.jsp">Back!</a>
	</body>
</html>